﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace QLHXNK
{
    public partial class FrmDangNhap : Form
    {
        public FrmDangNhap()
        {
            InitializeComponent();

            lblErrorNotice.Visible = false;
            this.CenterToScreen();
            
            
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void doLogin()
        {
            if (txtMatKhau.Text != "" && txtTenDangNhap.Text != "")
            {
                String strSQL = String.Format("SELECT Username FROM [User] WHERE Username = '{0}' AND Password = '{1}'", txtTenDangNhap.Text.Replace("'", "''"), txtMatKhau.Text.Replace("'", "''"));
                DataTable userTable = MyDataTransfer.executeQueryCommand(strSQL);

                if (userTable != null && userTable.Rows.Count == 1)
                {
                    MainForm.TenDangNhap = txtTenDangNhap.Text;
                    this.Close();//Đăng nhập thành công
                }
                else
                {
                    lblErrorNotice.Visible = true;
                    if (userTable == null)
                        lblErrorNotice.Text = "Lỗi kết nối cơ sở dữ liệu";
                    else
                        lblErrorNotice.Text = "Sai tên đăng nhập hoặc mật khẩu";
                    lblErrorNotice.BackColor = Color.Red;

                }
            }
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            doLogin();
        }

        private void FrmDangNhap_Load(object sender, EventArgs e)
        {

        }

        private void txtMatKhau_TextChanged(object sender, EventArgs e)
        {
            lblErrorNotice.Visible = false;
        }

        private void txtTenDangNhap_TextChanged(object sender, EventArgs e)
        {
            lblErrorNotice.Visible = false;
        }
    }
}